#!/bin/sh

# Slackware build script for espeak

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20200822 bkw:
# - BUILD=3
# - Fix PORTAUDIO=no builds, thanks to timsoft for reporting this.

# 20191218 bkw:
# - BUILD=2
# - Update man page (still using the one from Debian).
# - It turns out espeak supports PulseAudio, so that's now the default.
# - Remove portaudio from REQUIRES.
# - Add PULSE and PORTAUDIO environment variables, update README.
# - Simplify the script (unfortunately it had to get more complex
#   again to support the new variables).

PRGNAM=espeak
VERSION=${VERSION:-1.48.04}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

# Support old WAVONLY variable, but don't mention it in README.
if [ "${WAVONLY:-no}" = "yes" ]; then
  PULSE=no
  PORTAUDIO=no
fi

# Preemptively support -current's pure-alsa-system: if pulseaudio's
# not installed (it normally is on 14.2), just build without it.
PULSE="${PULSE:-yes}"
pkg-config --exists libpulse || PULSE=no

case "${PORTAUDIO:-auto}" in
  yes) if ! pkg-config --exists portaudio-2.0; then
         cat <<EOF
You've requested portaudio support via PORTAUDIO=yes, but portaudio
is not installed. Sorry.
EOF
         exit 1
       fi ;;
  no)  ;;
  *)   pkg-config --exists portaudio-2.0 && PORTAUDIO=yes || PORTAUDIO=no ;;
esac

if [ "$PORTAUDIO" = "yes" ] && [ "$PULSE" = "yes" ]; then
  AUDIO=runtime
  DRIVERS="portaudio and pulseaudio"
elif [ "$PORTAUDIO" = "yes" ]; then
  AUDIO=portaudio
  DRIVERS="$AUDIO"
elif [ "$PULSE" = "yes" ]; then
  AUDIO=pulseaudio
  DRIVERS="$AUDIO"
else
  AUDIO=none
  DRIVERS="none (.wav file output only)"
fi

echo "=== PULSE=$PULSE PORTAUDIO=$PORTAUDIO AUDIO=$AUDIO"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION-source
unzip $CWD/$PRGNAM-$VERSION-source.zip -x '*/linux_32bit/*' '*/platforms/*'
cd $PRGNAM-$VERSION-source
chown -R root:root .
# Permissions are bletcherous, reset them all.
find -L . \( -type d -a -exec chmod 755 {} + \) -o \
          \( -type f -a -exec chmod 644 {} + \)

# Note: there are two executables (speak and espeak). They seem to do the same
# thing, except "speak" doesn't use libespeak.so at runtime. This package only
# installs "espeak" (which is how other distros do it, too).
# The LDFLAGS="-Wl,-s" makes gcc strip the binaries for us.

cd src
  rm -f portaudio.h # use system-wide header!
  LIBDIR=/usr/lib$LIBDIRSUFFIX
  SLKCFLAGS="$SLKCFLAGS -Wno-narrowing"
  make LDFLAGS="-Wl,-s" LIBDIR=$LIBDIR CXXFLAGS="$SLKCFLAGS" AUDIO="$AUDIO"
  make install LIBDIR=$LIBDIR DESTDIR=$PKG AUDIO="$AUDIO"
  rm -f $PKG/$LIBDIR/*.a # guidelines say no static libs
cd ..

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -r ReadMe *.txt docs/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Man page from Debian
mkdir -p $PKG/usr/man/man1
gzip -9c $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

mkdir -p $PKG/install
sed "s,@DRIVERS@,$DRIVERS," \
  $CWD/slack-desc \
  > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
